var express = require('express');
var router = express.Router();
var connection = require('../backjs/node_mysql.js')
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
const SMSClient = require('@alicloud/sms-sdk');/// fym test

/* GET home page. */
router.use(session({
    resave: false, //添加 resave 选项
    saveUninitialized: true, //添加 saveUninitialized 选项
    secret: 'aF,.j)wBhq+E9n#aHHZ91Ba!VaoMfC', // 建议使用 128 个字符的随机字符串
    cookie: { maxAge: 20 * 1000 * 60 }  //ms

}))
router.get("/1", function (req, res, next) {
    res.render("add_Components_test")
})
router.get('/', function (req, res, next) {
    res.render('index')
});
router.get('/index.html', function (req, res, next) {
    res.render('index')
});
router.get('/top-menu.html', function (req, res, next) {
    res.render('top-menu.html')
});
router.get('/newpage-01.html', function (req, res, next) {
    res.render('newpage-01.html')
});
router.get('/projectManage.html', function (req, res, next) {

    res.render('projectManage.html')
});
router.post('/getMessage', function (req, res) {
    let sql = "SELECT * FROM project"
    connection.query(sql, function (err, rs) {
        if (err) {
            throw err
        }
        if (rs.length != 0) {
            console.log(rs)
            req.message = rs;
            res.send(rs)

        }
        else {
            res.send(400)
        }
    })
});


router.get('/deputeManage.html', function (req, res, next) {
    res.render('deputeManage.html');
});
router.get('/tecCardManage.html', function (req, res, next) {
    res.render('tecCardManage.html');
});
router.get('/checkTecCard.html', function (req, res, next) {
    res.render('checkTecCard');
});
router.get('/checkReport.html', function (req, res, next) {
    res.render('checkReport.html');
});
router.get('/info.html', function (req, res, next) {
    res.render('info.html');
});

router.get('/viewDepute.html', function (req, res, next) {
    res.render('viewDepute.html');
});
router.get('/newDepute.html', function (req, res, next) {
    res.render('newDepute.html');
});
router.get('/error-404.html', function (req, res, next) {
    res.render('error-404.html')
});
router.get('/error-500.html', function (req, res, next) {
    res.render('error-500.html')
});
router.get('/taskPaper.html', function (req, res, next) {
    res.render('taskPaper.html')
});
router.get('/newTecCard.html', function (req, res, next) {
    res.render('newTecCard.html');
});

router.get('/projectTemp.html', function (req, res, next) {
    // console.log(req.query.attr0)
    res.pj_id = req.query.attr0
    res.render('projectTemp.html')
});
router.get('/checkDepute.html', function (req, res, next) {
    res.render('checkDepute.html')
});
router.get('/consumerList.html', function (req, res, next) {
    res.render('consumerList.html')
});
router.get('/000.html', function (req, res, next) {
    res.render('000.html')
});
router.get('/head.html', function (req, res, next) {
    res.render('head.html')
});
router.get('/sidebar.html', function (req, res, next) {
    res.render('sidebar.html')
});
//用户的sidebar
router.get('/client', function (req, res, next) {
    res.render('client.html')
});
//业务部门sidebar
router.get('/business', function (req, res, next) {
    res.render('business.html')
});
//检测主管sidebar
router.get('/check_director', function (req, res, next) {
    res.render('check_director.html')
});
//检测人员sidebar
router.get('/check_worker', function (req, res, next) {
    res.render('check_worker.html')
});
//审核部门sidebar
router.get('/auditor', function (req, res, next) {
    res.render('auditor.html')
});
router.get('/login', function (req, res, next) {
    if (req.session.user != null) {
        res.render('index.html')
    }
    else {
        res.render('login.html')
    }
}).post('/login', function (req, res) {
    // console.log('????');
    let sql = 'select tmp.*,staff.* from (select * from user where user_account = ? and user_password = ?) tmp ' +
        'left join staff on user_id = staff_id';
    var params = [req.body.useraccount, req.body.userPwd];
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err
        }
        if (rs.length != 0) {
            req.session.user = rs[0];
            res.send(200);
        }
        else {
            res.send(400);
        }
    })
});



router.get("/checkuser", function (req, res, next) {
    if (req.session.user != null) {
        // console.log(req.session.user);
        res.json(req.session.user);
    }
    else {
        res.send(400);
    }
});
router.get('/addCheckDetail.html', function (req, res, next) {
    res.render('addCheckDetail.html')
}).post('/addCheckDetail', function (req, res) {
    res.send(200);///这句是临时加的，可以删除，等数据库对接上
});
router.get('/makeReport.html', function (req, res, next) {
    res.render('makeReport.html')
});
router.get('/evaluationRecord.html', function (req, res, next) {
    res.render('evaluationRecord.html')
});
router.get('/personManage.html', function (req, res, next) {
    res.render('personManage.html')
});
router.get('/profile.html', function (req, res, next) {
    res.render('profile.html')
});
router.get('/testDialog.html', function (req, res, next) {
    res.render('testDialog.html')
});
router.get('/newProject.html', function (req, res, next) {
    res.render('newProject.html');
});
router.get('/letterManage.html', function (req, res, next) {
    res.render('letterManage.html');
});
router.get('/groupManage.html', function (req, res, next) {
    res.render('groupManage.html');
});
// router.post('/new', function (req, res) {
//     var project_serial = req.body.project_serial;
//     var project_manage = req.body.project_manage;
//     // console.log("获取前端传递的数据")
//     // console.log(project_serial)
//     let sql = "INSERT INTO projects  (project_serial,project_status) VALUES (?,?)"
//     var upParmas = [project_serial, project_manage]
//
//
//     connection.query(sql, upParmas, function (err, re) {
//         if (err) {
//             throw err
//         }
//         if (re.length != 0) {
//             res.send(200)
//         }
//         else {
//             res.send(400)
//         }
//     })
// });

router.get('/shenhe-table.html', function (req, res, next) {
    res.render('shenhe-table.html')
});
router.get('/yewu-table.html', function (req, res, next) {
    res.render('yewu-table.html')
});
router.get('/user-table.html', function (req, res, next) {
    res.render('user-table.html')
});
router.get('/jiance-table.html', function (req, res, next) {
    res.render('jiance-table.html')
});

//修改用户的联系方式
router.get('/profile.html', function (req, res, next) {
    res.render('profile.html')
}).post('/profile', function (req, res) {
    if (req.body.requestType == 'getPhone') {
        //查询用户的usr_num
        var userId01 = req.body.userId
        console.log(userId01)
        let sql = 'SELECT user_phone FROM  user ' + 'WHERE user_id = ' + userId01;
        connection.query(sql, function (err, rs) {
            if (err) {
                throw err
            }
            if (rs.length != 0) {
                res.send(rs[0])
            }
            else {
                res.send(400)
            }
        })
    } else if (req.body.requestType == 'updatePhone'){
        var newNumber = req.body.newNumber
        var userId = req.body.userId
        let sql = 'update user set user_num = ? where user_id = ?'
        let params = [newNumber, userId]
        console.log(newNumber)
        connection.query(sql, params, function (err, rs) {
            if (err) {
                throw err
            }
            if (rs.length != 0) {
                res.send(200)
            }
            else {
                res.send(400)
            }
        })
    }
});
//处理修改密码
router.get('/changePwd.html', function (req, res, next) {
    res.render('changePwd.html')
}).post('/changePwd.html', function (req, res) {
    var newPwd = req.body.newPwd
    var userId = req.body.userId
    let sql = 'update user set user_password = ? where user_account = ?'
    let params = [newPwd, userId]
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err
        }
        if (rs.length != 0) {
            res.send(200)
        }
        else {
            res.send(400)
        }
    })
});
//用户注册
router.post('/register', function (req, res) {
    console.log(req.body)
    let sql = "insert into user (user_name, user_account, user_password) values (?,?,?)"
    let params = [req.body.userName, req.body.userAccount, req.body.userPwd]
    connection.query(sql, params, function (err, rs) {
        if (err) {
            throw err
        }
        if (rs.length != 0) {
            res.send(200)
        }
        else {
            res.send(400)
        }
    })
});
//处理登出
router.get('/loginout', function (req, res, next) {
    req.session.user = null
    res.send(200)
});

//调用阿里云的发送短信接口
router.post('/getCode', function (req, res) {
    // ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换
    const accessKeyId = 'LTAI5Sxs37KkXbJt'//'yourAccessKeyId'
    const secretAccessKey = 'EipyGNVVejnEHaYn9RroAJyyhSv3Fe'//'yourAccessKeySecret'
    //初始化sms_client
    let smsClient = new SMSClient({ accessKeyId, secretAccessKey })
    //发送短信
    smsClient.sendSMS({
        PhoneNumbers: req.body.inputPhone, //必填: 待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码, 批量调用相对于单条调用及时性稍有延迟, 验证码类型的短信推荐使用单条调用的方式；发送国际/港澳台消息时，接收号码格式为00+国际区号+号码，如“0085200000000”
        SignName: '冯懿明', //必填: 短信签名 - 可在短信控制台中找到
        TemplateCode: 'SMS_142050506', //必填: 短信模板 - 可在短信控制台中找到，发送国际/港澳台消息时，请使用国际/港澳台短信模版
        TemplateParam: '{"code":"12345"}'//可选: 模板中的变量替换JSON串, 如模板内容为"亲爱的${name},您的验证码为${code}"时。
    }).then(function (res) {
        let { Code } = res
        if (Code === 'OK') {
            //处理返回参数
            // console.log(res);
            res.send(200);
        }
    }, function (err) {
        console.log(err);
        res.send(400);
    })

});

router.get('/task_manage.html', function (req, res, next) {
    res.render('task_manage.html');
});
router.get('/report.html', function (req, res, next) {
    res.render('report.html');
});
router.get('/notificationManage.html', function (req, res, next) {
    res.render('notificationManage.html');
});
router.get('/ReportManage.html', function (req, res, next) {
    res.render('ReportManage.html');
});
router.get('/report-business', function (req, res, next) {
    res.render('report-business.html');
});
router.get('/createReport', function (req, res, next) {
    res.render('createReport.html');
});
router.get('/resultLetter', function (req, res, next) {
    res.render('resultLetter.html');
});
router.get('/viewProject.html', function (req, res, next) {
    res.render('viewProject.html');
});
router.get('/viewNotification.html', function (req, res, next) {
    res.render('viewNotification.html');
});
router.get('/viewGroup.html', function (req, res, next) {
    res.render('viewGroup.html');
});
router.get('/viewTecCard.html', function (req, res, next) {
    res.render('viewTecCard.html');
});
router.get('/viewReport.html', function (req, res, next) {
    res.render('viewReport.html');
});
router.get('/viewWorkOrder.html', function (req, res, next) {
    res.render('viewWorkOrder.html');
});
router.get('/workOrderManage.html', function (req, res, next) {
    res.render('workOrderManage.html');
});
router.get('/newGroup.html', function (req, res, next) {
    res.render('newGroup.html');
});
router.get('/viewWorkpieceType.html', function (req, res, next) {
    res.render('viewWorkpieceType.html');
});
router.get('/modifyTecCard.html', function (req, res, next) {
    res.render('modifyTecCard.html');
});
router.get('/allocateWorkpiece.html', function (req, res, next) {
    res.render('allocateWorkpiece.html');
});
router.get('/createOperation.html', function (req, res, next) {
    res.render('createOperation.html');
});
router.get('/viewOperation.html', function (req, res, next) {
    res.render('viewOperation.html');
});
router.get('/createReport.html', function (req, res, next) {
    res.render('createReport.html');
});
router.get('/authorityManage.html', function (req, res, next) {
    res.render('authorityManage.html');
});
router.get('/viewStaffPart.html', function (req, res, next) {
    res.render('viewStaffPart.html');
});
router.get('/departmentManage.html', function (req, res, next) {
    res.render('departmentManage.html');
});
router.get('/newDepartment.html', function (req, res, next) {
    res.render('newDepartment.html');
});
router.get('/viewAllUsers.html', function (req, res, next) {
    res.render('viewAllUsers.html');
});
router.get('/viewDepartment.html', function (req, res, next) {
    res.render('viewDepartment.html');
});
module.exports = router;